<?php
/**
 * Источник данных Select
 * 
 * @author naspeh
 * @version $Id: Select.php 658 2008-06-13 14:24:51Z naspeh $
 */
class Core_Record_Source_Select extends Core_Record_Source_Abstract 
{
    /**
     * Select
     *
     * @var Zend_Db_Select
     */
    protected $_select;
    /**
     * Конструктор
     *
     * @param Zend_Db_Select $source
     */
    public function __construct(Zend_Db_Select $source)
    {
        $this->_select = $source;
    }
    /**
     * Возвращает список доступных полей
     *
     * @return array
     */
    protected function _getFields()
    {
        $select = clone $this->_select;
        $fields = $select->limit(1)->query()->fetchAll();
        $fields = array_keys(current($fields));
        return $fields;
    }
    /**
     * Возвращает все стороки 
     *
     * @param string|array|Zend_Db_Table_Select $where  OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
     * @param string|array                      $order  OPTIONAL An SQL ORDER clause.
     * @param int                               $count  OPTIONAL An SQL LIMIT count.
     * @param int                               $offset OPTIONAL An SQL LIMIT offset.
     * @return array
     */
    public function fetchAll($where = null, $order = null, $count = null, $offset = null)
    {
        $select = $this->_select;
        if (isset($where)) {
        	$select->where($where);
        }
        
        if (isset($order)) {
        	$select->order($order);
        }
        if (isset($count) || isset($offset)) {
        	$select->limit($count, $offset);
        }            
        return $select->query()->fetchAll();       
    }
}